package swarch.messageflow;

import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.jbi.messaging.DeliveryChannel;
import javax.jbi.messaging.ExchangeStatus;
import javax.jbi.messaging.MessageExchange;
import javax.jbi.messaging.MessagingException;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.xml.transform.Source;

import org.apache.activemq.ActiveMQConnection;
import org.apache.log4j.Logger;
import org.apache.servicemix.components.util.ComponentSupport;
import org.apache.servicemix.jbi.jaxp.SourceTransformer;
import org.apache.servicemix.jbi.listener.MessageExchangeListener;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class AssessmentRepositoryBean extends ComponentSupport implements
		MessageExchangeListener {

	private final static Logger logger = Logger
			.getLogger(AssessmentRepositoryBean.class);
	private SourceTransformer sourceTransformer = new SourceTransformer();
	@Resource
	private DeliveryChannel channel;

	public void onMessageExchange(MessageExchange exchange)
			throws MessagingException {

		String strDestination = "swarch.assessment.output";

		logger.info("Destination: " + strDestination);

		Element payloadElem = null;
		Source contentSource = null;
		try {
			contentSource = getInMessage(exchange).getContent();
			// logger.info("received payload " +
			// sourceTransformer.toString(getInMessage(exchange).getContent()));
			payloadElem = sourceTransformer.toDOMElement(contentSource);
		} catch (Exception e) {
			logger.error("error while reading payload", e);
		}

		// get assessment Id
		// <messages><message-type>Assessment</message-type><assessment-id>textContent</assessment-id></messages>
		String assessId = payloadElem.getChildNodes().item(1).getTextContent();

		logger.info("assessId: " + assessId);
		Assessment a = AssessmentHelper.findAssessmentById(assessId);

		ActiveMQConnection connection = null;
		try {
			connection = ActiveMQConnection
					.makeConnection("tcp://localhost:61616");

			connection.start();
			Session session = connection.createSession(false,
					Session.AUTO_ACKNOWLEDGE);
			Destination destination = session.createQueue(strDestination);
			ObjectMessage simpleMessage = session.createObjectMessage();

			simpleMessage.setObject(a);

			MessageProducer producer = session.createProducer(destination);
			producer.send(simpleMessage);

			logger.info("send message to '" + strDestination + "'");
			if (a == null) {
			} else {
				logger.info("assess id: " + a.getAssessId());
			}

		} catch (JMSException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (URISyntaxException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		exchange.setStatus(ExchangeStatus.DONE);
		channel.send(exchange);
	}
}
